package com.example.myapplication.ui.Splashview;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.example.myapplication.R;
import com.example.myapplication.base.ActivityManager;
import com.example.myapplication.ui.Home.activity.MainActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class GuideActivity extends AppCompatActivity {
    @BindView(R.id.viewPager)
    ViewPager mViewPager;

    @BindView(R.id.white_point)
     ImageView mWhitePoint;
//
    @BindView(R.id.point_group)
    LinearLayout mPointGroup;

    @BindView(R.id.btn_submit)
    Button btn_submit;

    @BindView(R.id.btn_over)
    Button btn_over;
    private int mPointMargin;

    int [] mIcons={R.mipmap.uoko_guide_background_1,R.mipmap.uoko_guide_background_2,R.mipmap.uoko_guide_background_3};

    private List<ImageView> mImageList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_guide);
        ButterKnife.bind(this);
        mImageList = new ArrayList<>();
        for (int i = 0; i < mIcons.length; i++) {
            // 准备好显示的图片
            ImageView image = new ImageView(this);
            image.setBackgroundResource(mIcons[i]);
            // 添加到集合
            mImageList.add(image);
            ImageView point = new ImageView(this);
            point.setImageResource(R.drawable.shape_point_normal);

            int pointSize = getResources().getDimensionPixelSize(R.dimen.dimen_6dp);
            RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(pointSize, pointSize);
            mWhitePoint.setLayoutParams(params1);
            // 设置灰色点的布局参数
            LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(pointSize, pointSize);
            if (i > 0) {
                params2.leftMargin = getResources().getDimensionPixelSize(R.dimen.dimen_6dp);
            }
            point.setLayoutParams(params2);

            mPointGroup.addView(point);
        }

        // 设置适配器
        mViewPager.setAdapter(new GuideAdapter());
////        // 获取视图树对象，通过监听白点布局的显示，然后获取两个圆点之间的距离
        mWhitePoint.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @SuppressLint("NewApi")
            @Override
            public void onGlobalLayout() {
                // 此时layout布局已经显示出来了，可以获取小圆点之间的距离了
                mPointMargin = mPointGroup.getChildAt(1).getLeft() - mPointGroup.getChildAt(0).getLeft();

                // 将自己移除掉
                mWhitePoint.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        });
        /**
         * 对View Pager添加监听
         */
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                // 页面滑动的时候，动态的获取小圆点的左边距
                int leftMargin = (int) (mPointMargin * (position + positionOffset));
                // 获取布局参数，然后设置布局参数
                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mWhitePoint.getLayoutParams();
                // 修改参数
                params.leftMargin = leftMargin;
                // 重新设置布局参数
                mWhitePoint.setLayoutParams(params);
            }

            @Override
            public void onPageSelected(int position) {

                // 最后一页
                if (position == mIcons.length - 1) {
                    btn_over.setVisibility(View.GONE);
                    btn_submit.setVisibility(View.VISIBLE);
                } else if (position == 0){
                    // 不是最后一页
                    btn_over.setVisibility(View.VISIBLE);
                    btn_submit.setVisibility(View.GONE);
                }else {
                    btn_over.setVisibility(View.VISIBLE);
                    btn_submit.setVisibility(View.GONE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        // SKIP跳过按钮的点击事件
        btn_submit.setOnClickListener(v -> {
            startActivity(new Intent(this, MainActivity.class));
            ActivityManager.finishActivity(this);
        });


        // 跳过点击按钮的点击事件
        btn_over.setOnClickListener(v -> {
            startActivity(new Intent(this, MainActivity.class));
            ActivityManager.finishActivity(this);
        });

    }



     class  GuideAdapter  extends PagerAdapter {
        @Override
        public int getCount() {
            return mImageList.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            // 将图片添加到ViewPager容器
            container.addView(mImageList.get(position));
            return mImageList.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }
}
